//==============================================================================
#include "ComponentList.h"

using namespace ui;

string const type = "COMPONENTLIST";
//==============================================================================
ComponentList::ComponentList(int posX, int posY, int width, int height, COMPONENT_TYPE type, string name) : Component(posX, posY, width, height, type, name)
{
}
//------------------------------------------------------------------------------
ui::ComponentList::ComponentList( int showOpts, int x, int y, int w, int h, LISTCOMP* optNames): Component(x, y, w, h, COMPONENTLIST, "") {
	
	this->opciones = optNames;
	this->selectorActual.opcionActual = this->opciones->end();
	this->cantComp = showOpts;
	this->selectorPrimero.opcionActual = this->opciones->begin();
	LISTCOMP::iterator it = this->opciones->begin();
	if(this->opciones->size()>0){
		for (int i=0;i<(cantComp -2);i++){
			it++;
		}
		this->selectorUltimo.opcionActual = it;
	}
	this->type = COMPONENTLIST;
}
//------------------------------------------------------------------------------
ui::ComponentList::ComponentList( int showOpts, int x, int y, int w, int h): Component(x, y, w, h, COMPONENTLIST,"") {
	this->opciones = new LISTCOMP();
	this->selectorPrimero.opcionActual = this->opciones->begin();
	this->selectorActual.opcionActual = this->opciones->end();
	this->selectorUltimo.opcionActual = this->opciones->begin();
	this->cantComp = showOpts;
	this->type = COMPONENTLIST;
}
//------------------------------------------------------------------------------
void ui::ComponentList::addOption(ui::SceneObject* opt){
	if(!this->opciones->empty()){
		LISTCOMP::iterator end = this->opciones->end();
		end--;
		opt->setXCoordinate((*end)->getXCoordinate());
		opt->setYCoordinate((*end)->getYCoordinate()+(*end)->getHeight() + 10);
	} else {
		opt->setXCoordinate(this->getXCoordinate() + opt->getWidth() / 2);
		opt->setYCoordinate(this->getYCoordinate() + opt->getHeight() / 2);
	}

	this->opciones->push_back(opt);
	if(this->opciones->size() == 1){
		this->selectorPrimero.opcionActual = this->opciones->begin();
		this->selectorUltimo.opcionActual = this->opciones->begin();
	}
	//If not empty nor full , update last selector.
	if(this->opciones->size() <= (this->cantComp) && this->opciones->size()>1 ) {
		this->selectorUltimo.opcionActual++;
	}
}
//------------------------------------------------------------------------------
COMPONENT_TYPE ui::ComponentList::handleEvent(SDL_Event* event){
	COMPONENT_TYPE	varType = NONE;
	if(event->type == SDL_MOUSEBUTTONDOWN && event->button.button == SDL_BUTTON_LEFT && this->ocurredInside(event->button.x, event->button.y)){
		list<SceneObject*>::iterator it = this->selectorPrimero.opcionActual;
		int count = 0;
		while( it !=this->opciones->end() && !( this->opciones->empty() ) && count < this->cantComp ) {
			if((*it)->ocurredInside(event->button.x, event->button.y)) {
				//If pressed save the iterator.
				this->selectorActual.opcionActual = it;
				varType = this->type;
				break;
			}
			else{
				it++;
				count++;
			}
		}
	}
	return varType;
}
//------------------------------------------------------------------------------
void ui::ComponentList::draw(SDL_Surface* s){
    try
    {
        if(!this->opciones->empty()){
            LISTCOMP::iterator it = this->selectorPrimero.opcionActual;
            (*it)->draw(s);
            while(it != this->selectorUltimo.opcionActual){
                it++;
                (*it)->draw(s);
            }
        }
    }
    catch(SDL_Exception &ex)
    {
        throw ex;
    }
}
//------------------------------------------------------------------------------
ui::SceneObject* ui::ComponentList::getObject(){
	SceneObject* result = NULL;
	if(this->selectorActual.opcionActual != this->opciones->end()){
		int newX = (*this->selectorActual.opcionActual)->getXCoordinate();
		int newY = (*this->selectorActual.opcionActual)->getYCoordinate();
		SceneObject* newObject;
		switch((*this->selectorActual.opcionActual)->getType()){
		case(SCENEOBJECT):{
				 newObject = new SceneObject(newX, newY,(*this->selectorActual.opcionActual)->getName());
						  }
				break;
		case(MINE):{
				 newObject = new Mine(newX, newY,0);
				   }
				   break;		
		}
		newObject->loadSurface();
		result = newObject;

	};
	this->selectorActual.opcionActual = this->opciones->end();
return result;
}
//------------------------------------------------------------------------------
void ui::ComponentList::scrollUp(){
	if ((this->opciones->size()>cantComp) && (this->selectorPrimero.opcionActual != this->opciones->begin())) {
		this->selectorPrimero.opcionActual--;
		this->selectorUltimo.opcionActual--;
		int increment = (*this->selectorPrimero.opcionActual)->getHeight() + 10;
		LISTCOMP::iterator it = this->opciones->begin();
		while(it != this->opciones->end()){
			(*it)->setYCoordinate((*it)->getYCoordinate()+increment);
			it++;
		}
	}
}
//------------------------------------------------------------------------------
void ui::ComponentList::scrollDown(){
	LISTCOMP::iterator end(this->opciones->end());
	if(!this->opciones->empty())
		--end;
	//Verify if theres something to scroll on.
	if ((this->opciones->size()>cantComp) && (this->selectorUltimo.opcionActual != end)) {
		this->selectorPrimero.opcionActual++;
		this->selectorUltimo.opcionActual++;
		int increment = (*this->selectorPrimero.opcionActual)->getHeight() + 10;
		LISTCOMP::iterator it = this->opciones->begin();
		while(it != this->opciones->end()){
			(*it)->setYCoordinate((*it)->getYCoordinate()-increment);
			it++;
		}

	}
}
//------------------------------------------------------------------------------
ui::ComponentList::~ComponentList(void)
{
	while(!this->opciones->empty()){ 
		delete this->opciones->back(); 
		this->opciones->pop_back();
	};
	delete this->opciones;
}
//==============================================================================